﻿
#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Navigation;

#endregion

namespace FasterWPF
{
    public class TabControlArgs
    {
    }

    public class CustomTabItem : TabItem //, INotifyPropertyChanged 
    {
        public ContentControl CustomHeader { get; set; }
        public CustomTabItem()
            : base()
        {
        }

        public CustomTabItem(ContentControl header, TabItem content)
            : base()
        {
            this.Name = "Custom" + content.Name;
            CustomHeader = header;
            this.Content = content.Content;
            this.Tag = content.Tag;
            this.Template = content.Template;
        }
    }

    public class TabControlParent : FasterWPF.IParent 
    {         
        //this is ANA?
        public object Generate<T>(object chain, T args, Action<ContentControl> addUIElementsAction)
        {
            return chain;
        }

        #region IParent

        //this is BIND
        public object Add<T>(object chain, string key, T args, Border itemToAdd)
        {
              TabItem currentTab = itemToAdd.Child.Cast<ContentControl>().GetElement<CustomTabItem, TabControl>(0, 0);

              CustomTabItem currentCustomTab = currentTab as CustomTabItem;

              Binding b = new Binding();
              b.Source = currentCustomTab;
              b.Path = new PropertyPath("CustomHeader");

              currentTab.SetBinding(TabItem.HeaderProperty, b);

              ((TabControl)chain).GetSetting<TabControl, ObservableCollection<CustomTabItem>>("ObsCol").Add(currentCustomTab);

              var collection = ((TabControl)chain).GetSetting<TabControl, ObservableCollection<CustomTabItem>>("ObsCol");
              if (((TabControl)chain).ItemsSource != collection)
              {
                  ((TabControl)chain).ItemsSource = collection;
              }

            return chain;
        }

        public void Remove(object chain, string key, Border itemToRemove)
        {
            CustomTabItem currentCustomTab = ((TabControl)chain).GetSetting<TabControl, ObservableCollection<CustomTabItem>>("ObsCol").Where(custTab => {
                return custTab.Name == ((string)Application.Current.Properties["TabToBeRemoved"]);
            }).First() as CustomTabItem;

            ((TabControl)chain).GetSetting<TabControl, ObservableCollection<CustomTabItem>>("ObsCol").Remove(currentCustomTab.Cast<CustomTabItem>());

            IComposite ic = (IComposite)(((TabControl)chain).GetIComposite<TabControl>());
            ic.RemoveFromChain(itemToRemove);
        }

        public Dictionary<string, object> RetrieveDictionary(object chain)
        {
            return ((TabControl)chain).GetCompositeLookupDictionary<TabControl>();
        }

        //this is CATA?
        public List<Border> RetrieveCollection(object chain)
        {
            List<Border> lb = new List<Border>();
            foreach (var kvp in ((TabControl)chain).GetCompositeLookupDictionary<TabControl>())
            {
                lb.Add((Border)kvp.Value);
            }

            return lb;
        }

        #endregion

        public void ClearCollection(object chain)
        {
        }

        public void BeforeSortCollection<T>(object chain, object args, int row, int column, FasterWPF.CommonExt.SortOrder sortOrder)
                  where T : FrameworkElement
        { }

        public void BeforeSortCollection1Then2<T>(object chain, object args, int row, int column, int row2, int column2, FasterWPF.CommonExt.SortOrder sortOrder, FasterWPF.CommonExt.SortOrder sortOrder2)
  where T : FrameworkElement
        { }
        
        public void AfterSortCollection<T>(object chain)
             where T : FrameworkElement
        { }

        public void InnerSortCollection(object chain, int row, int column, string key, ref int counter, Border itemToReAdd)
        {
            throw new NotImplementedException();
        }
        
        public void SetSelected(object chain, Border itemToSetAsSelected)
        {
        }

        //public void GetSelected
        //    return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);

    }
}
